﻿<%@ WebHandler Language="C#" Class="UnqualifiedReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 不合格品审理查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.4.15
 **/

public class UnqualifiedReport : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT cci.familylistnames,cni.matnr productname,cci.containername,
       cni.zbhgdh,
       csr.reportemployeename worker,
       e2.fullname checker,
       cni.checkdate  dealDate,
       cni.createdate checkDate,
       cni.zsljl dealResult,
       cni.fecod unqualifiedReason,
       CASE WHEN cni.zbhglb = '0' THEN '0类不合格品'
             WHEN cni.zbhglb = 'Ⅰ' THEN 'Ⅰ类不合格品'
             WHEN cni.zbhglb = 'Ⅱ' THEN 'Ⅱ类不合格品'
             WHEN cni.zbhglb = 'Ⅲ' THEN 'Ⅲ类不合格品' 
       END unqualifiedType,
       ws.workflowstepname,
       cni.zbhgps qty
FROM containernonconforminginfo cni
LEFT JOIN containercurrentinfo cci ON cni.containerid = cci.containerid
LEFT JOIN containersteprecord csr ON csr.containerid = cni.containerid AND csr.workflowstepid = cni.workflowstepid
LEFT JOIN employee e ON e.employeeid = cni.employeeid
LEFT JOIN employee e2 ON e2.employeeid = cni.checkemployeeid
LEFT JOIN Workflowstep ws ON ws.workflowstepid = cni.workflowstepid
WHERE cci.containerstatus = 1  AND cni.isuse = 0
";
        //分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames = '{0}'", jObject["productfamilyname"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname LIKE '%{0}%'", jObject["productname"].ToString());
        }

        //流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername LIKE '%{0}%'", jObject["containername"].ToString());
        }

        //不合格品审理单号
        if (jObject["unqualifiedno"] != null && jObject["unqualifiedno"].ToString() != "")
        {
            sql += string.Format(" AND cni.zbhgdh LIKE '%{0}%'", jObject["unqualifiedno"].ToString());
        }

        //检验员
        if (jObject["chercker"] != null && jObject["chercker"].ToString() != "")
        {
            sql += string.Format(" AND cni.checkemployeeid = '{0}'", jObject["chercker"].ToString());
        }

        //处理时间
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(cni.checkdate) BETWEEN TO_DATE('{0}','yyyy-mm-dd') and TO_DATE('{1}','yyyy-mm-dd')", arr[0].ToString(), arr[1].ToString());
            }
        }

        //不合格品审理类型
        if (jObject["unqualifytype"] != null && jObject["unqualifytype"].ToString() != "")
        {
            sql += string.Format(" AND cni.zbhglb='{0}'", jObject["unqualifytype"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getSearchSqlString(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["familylistnames"].Caption = "机型";
        dtResult.Columns["productname"].Caption = "零件图号";
        dtResult.Columns["containername"].Caption = "流水卡号";
        dtResult.Columns["zbhgdh"].Caption = "不合格品单号";
        dtResult.Columns["worker"].Caption = "操作工";
        dtResult.Columns["checker"].Caption = "检验员";
        dtResult.Columns["dealdate"].Caption = "处理时间";
        dtResult.Columns["checkdate"].Caption = "检验时间";
        dtResult.Columns["dealresult"].Caption = "处理结果";
        dtResult.Columns["unqualifiedreason"].Caption = "不合格原因";
        dtResult.Columns["unqualifiedtype"].Caption = "不合格品类型";
        dtResult.Columns["workflowstepname"].Caption = "工序";
        dtResult.Columns["qty"].Caption = "数量";

        ExportHelper.ExportExcel(dtResult, "不合格品审理查询报表");
    }
}